<template>
	<view>
		<view class="ts" v-if="type == 'hobby'">
			最多可选3项
		</view>
		<view class="tipflex">
			<view class="item" v-for="(item,index) in list" :key="index" :class="item.default?'itemact':''"
				@click="change(item)">
				{{item.dictLabel}}
			</view>
		</view>
		<view class="footerbtn" @click="handleBc">
			<view class="cont">
				<view class="btn">
					保存
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getExtend,
		ExtendUpdate,
		getdict
	} from '@/common/http.api.js';
	export default {
		data() {
			return {
				list: [],
				selectList: [],

				type: ''
			};
		},
		onLoad(query) {
			if(query.nowtype!='null'){
				this.selectList =  query.nowtype.split(',') 
			}else{
				this.selectList = []
			}
			let title = ''
			this.type = query.type
			if (query.type == 'career') {
				title = '选择职业'
			} else if (query.type == 'source') {
				title = '注册来源'
			} else {
				title = '兴趣爱好'
			}
			uni.setNavigationBarTitle({
				title
			})
			getdict(query.dict).then(res => {
				this.list = res.data
				this.selectList.forEach(i=>{
					this.list.filter(item=>{
						if(item.dictValue == i){
							item.default = true
						}
					})
				})
				
			})
		},
		methods: {
			handleBc() {
				console.log(this.type,this.selectList);
				ExtendUpdate({
					[this.type]:this.selectList.join(',')
				}).then(res=>{
					console.log(res);
					uni.navigateBack()
				})
			},
			//单选
			change(item) {
				if (this.type != 'hobby') {
					this.list.forEach(item => {
						item.default = false
					})
					item.default = !item.default
					let ulist = this.list.filter(item => {
						return item.default
					})[0].dictLabel
					this.selectList = []
					this.selectList.push(ulist)
				} else {
					item.default = !item.default
					let index = this.selectList.indexOf(item.dictLabel)
					let select = index >= 0
					if (select) {
						this.selectList.splice(index, 1)
					} else {
						if (this.selectList.length >= 3) {
							this.list.find(obj => obj.dictLabel == this.selectList[0]).default = false
							this.selectList.splice(0, 1)
						}
						this.selectList.push(item.dictLabel)
					}
				}
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #F5F5F5;
	}

	.ts {
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 30rpx;
		color: #979797;
		padding: 40rpx 40rpx 0rpx 40rpx;
	}

	.tipflex {
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		padding: 40rpx;
		box-sizing: border-box;

		.item {
			width: 210rpx;
			height: 76rpx;
			background: #FFFFFF;
			border-radius: 8rpx 8rpx 8rpx 8rpx;
			line-height: 76rpx;
			margin-right: 20rpx;
			margin-bottom: 20rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #000000;
			text-align: center;
		}

		.item:nth-child(3n) {
			margin-right: 0;
		}

		.itemact {
			background: #000000 !important;
			color: #FFFFFF !important;
		}
	}

	.footerbtn {
		width: 750rpx;
		height: 120rpx;
		background: #FFFFFF;
		position: fixed;
		bottom: 0%;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);

		.cont {
			display: flex;
			padding: 10rpx 20rpx;
			height: 120rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			align-items: center;
			justify-content: center;

			.btn {
				width: 670rpx;
				height: 100rpx;
				background: #000000;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				color: #FFFFFF;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 32rpx;
				color: #FFFFFF;
				line-height: 100rpx;
				text-align: center;
			}

			.absbtn {
				background: #808080;
			}
		}
	}
</style>